package com.hh.mads.ui;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.hh.core.XDADService;
import com.hh.core.ad.XDActivityIconView;
import com.hh.mads.R;
import com.just.agentweb.AgentWeb;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created by YuHong on 2020/7/23 0023.
 */
public class MainActivity extends AppCompatActivity {

    private FloatingActionButton fab;
    private AgentWeb mAgentWeb;
    private LinearLayout layout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.aty_main);
        initUI();
    }

    private void initUI() {
        fab = findViewById(R.id.fab_get_money);
        layout = findViewById(R.id.ll_news);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XDActivityIconView view = new XDActivityIconView(MainActivity.this);
                view.goRewardPage();
            }
        });
        initWeb();
    }


    private void initWeb(){
        mAgentWeb = AgentWeb.with(this)
                .setAgentWebParent( layout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
                .useDefaultIndicator()
                .createAgentWeb()
                .ready()
                .go("http://www.lofter.com/");
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("anim_test","onTouchEvent");
        if(event.getAction()==MotionEvent.ACTION_MOVE){
            fabMoveAndHint();
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            fabMoveAndShow();
                        }
                    });
                }
            }, 1000);
        }
        return true;
    }


    private void fabMoveAndHint() {
        TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,0f,
                Animation.ABSOLUTE,50f,
                Animation.ABSOLUTE,0f,
                Animation.ABSOLUTE,0f);
        animation.setDuration(500);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e("anim_test","开始执行动画");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.e("anim_test","over动画");
                fab.setAlpha(0.3f);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.e("anim_test","Repeat动画");
            }
        });
        animation.setFillAfter(true);
        animation.setRepeatCount(0);
        animation.setRepeatMode(Animation.RESTART);
        fab.startAnimation(animation);
    }

    private void fabMoveAndShow() {
        TranslateAnimation animation = new TranslateAnimation(Animation.ABSOLUTE,50f,
                Animation.ABSOLUTE,0f,
                Animation.ABSOLUTE,0f,
                Animation.ABSOLUTE,0f);
        animation.setDuration(500);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.e("anim_test","开始显示动画");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.e("anim_test","结束动画2");
                fab.setAlpha(1.0f);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.e("anim_test","onAnimationRepeat");
            }
        });
        animation.setFillAfter(true);
        animation.setRepeatCount(0);
        animation.setRepeatMode(Animation.RESTART);
        fab.startAnimation(animation);
    }
    @Override
    public void onPause() {
        mAgentWeb.getWebLifeCycle().onPause();
        super.onPause();

    }

    @Override
    public void onResume() {
        mAgentWeb.getWebLifeCycle().onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAgentWeb.getWebLifeCycle().onDestroy();
    }

}
